package de.unistuttgart.iaas.interaction.skype.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * This class defines an object to calculate the time limit.
 * @author Lina
 *
 */
public class TimeoutComputer 
{	

	/**
	 * This method get the current system time as sendTime.
	 * @return sendTime
	 */
	public static String getSendTime()
	{		
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		Calendar cal = Calendar.getInstance();
		String sendTime = df.format(cal.getTime());
		return sendTime;
	}
	
	/**
	 * This method calculates the time limit according to parameter timeout and current system time.
	 * @param timeout
	 * @return
	 */
	public static String setTimeOut(String timeout)
	{
		Calendar cal = Calendar.getInstance();
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		
		String[] temp = timeout.split(":");
		
		cal.add(Calendar.SECOND, Integer.parseInt(temp[3]));
		cal.add(Calendar.MINUTE, Integer.parseInt(temp[2]));
		
		cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(temp[1]));
		cal.add(Calendar.DATE, Integer.parseInt(temp[0]));

		String new_timeout = df.format(cal.getTime());
		return new_timeout;
		
	}
	
	public static boolean isTimeout(String timeout, String receivedDate)
	{
		boolean isTimeout=false;
		
		
		DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try
        {
        	c1.setTime(df.parse(receivedDate));
            c2.setTime(df.parse(timeout));
            
        }catch(java.text.ParseException e){
            System.err.println("invalid format");
        }
        int result=c1.compareTo(c2);
        if(result>0)
        	isTimeout=true;
            
		return isTimeout;
	}

}
